
// const baseUrl = "http://jiaoyou.zheyousoft.cn/api/app"// 本机开发测试地址              
// const baseUrl = "http://192.168.1.16:9312"// 本地

const baseUrl ="https://xiuqiu.xiuqiu.cc/api/app" //打包上线地址

//带Token请求 
export const request = (opts) => {
	//此token是登录成功后后台返回保存在storage中的
	const token = uni.getStorageSync('token');    //获取token
	//const token = userInfo.token  //获取token
	
/* 	let token = "";
	uni.getStorage({
		key: 'token',
		success: function(ress) {
			token = ress.data
		}
	}); */
	
	let httpDefaultOpts = {
		url: baseUrl + opts.url,
		method: opts.method,
		data:opts.data,
		// header: {
		// 	'Authorization': 'bearer '+token
		//  	},
		dataType: 'json',
	}
	if(token){
		httpDefaultOpts.header={
				'Authorization':'bearer '+token||'',
		}
	}
	
	console.log(httpDefaultOpts);
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1])
				console.log(res,1111);
				//console.log("res1==="+res[1].statusCode);
				let code = res[1].statusCode;
				console.log("状态码"+code)
				if(code==400){
					uni.showToast({
						title:res[1].data||'请求失败',
						icon:'none'
					})
				}
				if(code == 401){
					uni.addInterceptor('showModal', {})
				 
						console.log('调用了')
						uni.showModal({
							title: '提示',
							content: '你尚未登录,登录体验更佳哦',
							cancelColor: '#fe5353',
							confirmColor:'#008a00', 
							success: function (res) {
								if (res.confirm) {
									console.log('用户点击确定');
									uni.removeStorageSync('token')
									uni.reLaunch({
										url: '/pages/login/login'
									})
									return;
								} else if (res.cancel) {
									console.log('用户点击取消');
									uni.navigateBack();
									return;
								}
							}
						});
					
					
				/* 	uni.showModal({
						title: '提示',
						content: "授权过期,请重新登录",
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								uni.redirectTo({
									url: '../login/login'
								})
							}
						}
					}) */
					//return
				}else{
					uni.hideLoading()
				}
			}
		).catch(
			(response) => { 
				//console.log("response===="+response.statusCode);
				reject(response)
			}
		)
	})
	return promise
};